<html>
<head>
	<title>Upload Pipe - Test document</title>
	<script src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject_src.js" type="text/javascript"></script>
</head>
<body bgcolor="#dddddd">

<div id="upload-pipe-wrapper">
  This text is replaced by the Flash movie.
</div>

<script type="text/javascript">

function PipeMock(config) {
	
	this.config = config;
	
	this.select = function(id, response) {
		alert("message from id:"+id+" fileId: "+response.fileId+" filename: "+response.name+"; size: "+response.size);
	}
	
	this.pipe = new function() {
		
		this.name = null;
		
		this.remove = function(id) {
			return Pipe.Request.remove(this.name, id);
		}
		
		this.remove3 = function(id) {
			Pipe.Request.remove(this.name, id);
		}
		
		this.remove2 = function(id) {
			Pipe.Request.remove(this.name, id);
		}
	}
	
	this.init = function(config) {
		
		var flashvars = {
			id: config.id
		};

		var params = {
		};
		
		var attributes = {
		  id: config.id,
		  name: config.id
		}; 
		
		swfobject.embedSWF(
			config.movie, 
			config.id, 
			config.width, 
			config.height, 
			"9.0.0", 
			null,
			flashvars, 
			params, 
			attributes
		);

	}
	
	this.init(config);
	this.pipe.name = this.config.id;
	
	Pipe.addInstance(this);
}

var Pipe = {
	
	instance: [],
	
	Event: {
		select: function(id, fileId, name, size) {
			var response = {fileId: fileId, name: name, size: size};
			Pipe.find(id).select(id, response);
		},
		cancel: function(id) {
			alert("shit! cancel clicked for id: "+id);
		},
		uploadStart: function() {
		},
		uploadProgress: function() {
		},
		uploadComplete: function() {
		},
		error: function() {
		}
	},
	
	Request: {
		fileList: function() {
		},
		remove: function(movie, id) {
			//alert("deleting: "+id+" for movie "+movie);
			var pipe = Pipe.find(movie).config.id;
			return document.getElementById(pipe).remove(id);
		},
		clear: function() {
		},
		upload: function(idList) {
			if(!idList) {
				idList = [];
			}
			var pipe = Pipe.find(movie).config.id;
			return document.getElementById(pipe).upload(idList);
		}
	},
	
	addInstance: function(instance) {
		this.instance.push(instance);
	},
	
	find: function(id) {
		for(var i in this.instance) {
			if(this.instance[i].pipe.name == id) {
				return this.instance[i];
			}
		}
	},
	
	// @todo: remove this
	 
	sendToJavaScript: function(value)
	{
		alert("ActionScript says: " + value);

	},
	
	sendToActionScript: function(value) {
		thisMovie("haxe").sendToActionScript(value);
	}
}
  
var flashvars = {
  
  autoStart: false,
  allowMulti: true,
  
  uploadTo: "http://example.com",
  mouseOverImage: "mouseOver.jpg",
  mouseOutImage: "mouseOut.jpg",
  buttonLabel: "Browse ...",
  
};

var mock = new PipeMock({
	id: "upload-pipe-wrapper",
	movie: "haxe/upload-pipe.swf",
	width: 300,
	height: 120,
});
mock.del = function(id) {
	var x = this.pipe.remove(id);
	if(x) {
		var e = document.getElementById(this.pipe.name+"-"+id);
		e.parentNode.removeChild(e);
	}
	return false;
}
mock.select = function(id, response) {
	var files = document.getElementById("files");
	html = "<li id=\""+id+"-"+response.fileId+"\">";
	html+= response.name+" ("+response.size+" B)";
	html+= " <a href=\"#\" onclick=\"mock.del('"+response.fileId+"')\">remove</a>";
	html+= "</li>";
	
	files.innerHTML += html;
}
mock.upload = function() {
	this.pipe.upload([]);
}





</script>

     <form name="form1" onsubmit="return false;">
         <input type="text" name="input" value="" />
         <input type="button" value="Send" onclick="mock.upload()" /><br />
     </form>
	 
	 <ul id="files">
	 
	 </ul>
 

</body>
</html>
